from asyncio import constants
from flask import request, jsonify
from SQLOperation.PatientSQLOperation import PatientSQLOperation
from SQLConfig import app
from routes.deletePatient import deletePatient, deletep
from routes.newPatient import newp
import os, constants

@app.route('/getAllPatients', methods=['Get', 'POST'])
def getAllPatients():  # put application's code here
    """
    搜索全部病人
    """
    username = request.json.get("username")

    patients = PatientSQLOperation.getAllPatients(username)
    if username=="admin":
        initadmin(patients)
        return jsonify([{
            "id": "116",
            "name": "test",
            "gender": "男",
            "age": "23",
            "time": "123",
            "phone": "183xxxx4153",
            "username": "admin"
        }])
        


    res = []
    for patient in patients:
        res.append({
            "id": patient.id,
            "name": patient.name,
            "gender": patient.gender,
            "age": patient.age,
            "time": patient.time,
            "phone": patient.phone,
            "username": patient.username
        })
    return jsonify(res)

def initadmin(patients):
    for patient in patients:
        if (patient.name!="test"):
            deletep(str(patient.id))
        else:
            paturl=os.path.join(constants.patientBaseUrl,"116")
            if os.path.exists(paturl):
                os.popen("rm -rf "+paturl)
                os.popen("mkdir "+paturl)
                os.popen("cp -r "+"/root/testpcl/116/ "+ "/root/upload/")
